package com.nsu.cl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nsu.cl.pojo.CouresSelection;
import com.nsu.cl.service.CouresSelectionService;
import com.nsu.cl.service.GradeService;
import com.nsu.cl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "科目选择控制器")
@RestController
@RequestMapping("/couresSelectionController")
public class CouresSelectionController {

    @Autowired
    private CouresSelectionService couresSelectionService;

    @Autowired
    private GradeService gradeService;

    @ApiOperation("删除CouresSelection信息")
    @DeleteMapping("/deleteCouresSelection")
    public Result deleteCouresSelection(@ApiParam("要删除的所有的couresSelection的id的JSON集合") @RequestBody List<Integer> ids) {
        // 调用服务层
        couresSelectionService.removeByIds(ids);

        return Result.ok();
    }

    @ApiOperation("新增或修改Achievements，有id属性则是修改，没有则是增加")
    @PostMapping("/saveOrUpdateCouresSelection")
    public Result saveOrUpdateCouresSelection(@ApiParam("JSON的CouresSelection对象")@RequestBody CouresSelection couresSelection) {
        // 接收参数
        // 调用服务层方法完成增加或者修改
        couresSelectionService.saveOrUpdate(couresSelection);
        return Result.ok();
    }

    @ApiOperation("查询根据科目名称模糊查询，带分页")
    @GetMapping("/getCouresSelection/{pageNo}/{pageSize}")
        public Result getCouresSelection(@ApiParam("分页查询页码数")@PathVariable("pageNo") Integer pageNo,
                                  @ApiParam("分页查询页大小")@PathVariable("pageSize")Integer pageSize,
                                  @ApiParam("分页查询的条件")CouresSelection couresSelection) {
        // 分页 带条件查询
        Page<CouresSelection> page = new Page<>(pageNo,pageSize);
        // 通过服务层
        IPage<CouresSelection> iPage = couresSelectionService.getCouresSelectionByOpr(page,couresSelection);

        // 封装Result对象并返回
        return Result.ok(iPage);
    }

    @ApiOperation("查询ID查详情")
    @GetMapping("/getCouresSelectionById/{id}")
    public Result getCouresSelectionById(@ApiParam("课程选择ID")@PathVariable("id") Long id) {
        CouresSelection selection = couresSelectionService.getCouresSelectionById(id);
        selection.setGrade(gradeService.getGradeByName(selection.getTeacher().getGradeName()));
        return Result.ok(selection);
    }
}
